const RouterHook = require("../module/routerModule")
const { addCar,updateCar,deleteCar,getCarList } = require("../service/adminCar")
const { AssertUtils } = require("../util/utils")
const { query } = require("../util/dbutil")

const router = RouterHook()

/**
 * @swagger
 * /admin/car/getList:
 *    get:
 *      tags:
 *      - 管理后台车辆
 *      summary: 获取车辆列表
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 车辆id
 *          required: false
 *          type: number
 *        - name: license_plate
 *          description: 车牌号 支持模糊查询
 *          required: false
 *          type: string
 *        - name: vehicle_model
 *          description: 车辆型号 支持模糊查询
 *          required: false
 *          type: string
 *      responses:
 *        200:
 *          description: 返回车辆列表
 *          schema:
 *            type: object
 *            properties:
 *              data:
 *                type: array
 *                default: [
 *                    {
 *                      "id":1,
 *                      "user_id": 1,
 *                      "km_kw": 1231,
 *                      "battery_level": 80,
 *                      "license_plate": "京A1231",
 *                      "vehicle_model": 11,
 *                   }
 *                ]
 *        401:
 *          description: 参数错误
 * */
router.get("/getList", async (ctx) => {
    const q = ctx.request.query
    if(q.user_id){
        const res = await query(`select username from user where id=?`,[q.user_id])
        if(res.length<1){
            ctx.result.error('用户不存在')
        }
    }
    const {data,result} = await getCarList(q)
    ctx.body = {
        code:200,
        data,
        total:result[0].count
    }
})

/**
 * @swagger
 * /admin/car/add:
 *    post:
 *      tags:
 *      - 管理后台车辆
 *      summary: 新增车辆
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: user_id
 *          description: 所属车辆用户id
 *          required: true
 *          type: number
 *        - name: license_plate
 *          description: 车牌号
 *          required: true
 *          type: string
 *        - name: vehicle_model
 *          description: 车辆型号
 *          required: true
 *          type: string
 *        - name: battery_level
 *          description: 电池电量
 *          required: false
 *          type: number
 *        - name: km_kw
 *          description: 汽车公里油耗
 *          required: false
 *          type: number
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              response:
 *                type: object
 *                default: {
 *                      "code":200,
 *                      "message":"添加成功",
 *                   }
 *        401:
 *          description: 参数错误
 * */
router.post("/add", async (ctx) => {
    const body = ctx.request.body
    AssertUtils.StringNotEmpty(body.user_id, "请输入车辆所属用户id")
    const res = await query(`select username from user where id=?`,[body.user_id])
    console.log(res)
    if(res.result.length<1){
        return ctx.result.error(401,"用户不存在")
    }
    await addCar(body)
    ctx.body = {
        code:200,
        message:'添加成功'
    }
    ctx.result.success('添加成功')
})

/**
 * @swagger
 * /admin/car/update:
 *    post:
 *      tags:
 *      - 管理后台车辆
 *      summary: 修改车辆
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 车辆id
 *          required: true
 *          type: number
 *        - name: user_id
 *          description: 车辆所属用户id
 *          required: false
 *          type: number
 *        - name: license_plate
 *          description: 车牌号
 *          required: false
 *          type: string
 *        - name: vehicle_model
 *          description: 车辆型号
 *          required: false
 *          type: string
 *        - name: battery_level
 *          description: 电池电量
 *          required: false
 *          type: number
 *        - name: km_kw
 *          description: 汽车公里油耗
 *          required: false
 *          type: number
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              response:
 *                type: object
 *                default: {
 *                      "code":200,
 *                      "message":"修改成功",
 *                   }
 *        401:
 *          description: 参数错误
 * */
router.post("/update", async (ctx) => {
    const body = ctx.request.body
    AssertUtils.StringNotEmpty(body.id, "请输入车辆id")
    await updateCar(body)
    ctx.result.success([],'修改成功')
})
/**
 * @swagger
 * /admin/car/delete:
 *    post:
 *      tags:
 *      - 管理后台车辆
 *      summary: 删除车辆
 *      produces:
 *      - application/json
 *      parameters:
 *        - name: id
 *          description: 车辆id
 *          required: required
 *          type: number
 *      responses:
 *        200:
 *          description: 返回信息
 *          schema:
 *            type: object
 *            properties:
 *              response:
 *                type: object
 *                default: {
 *                    "code":200,
 *                    "message":"删除成功"
 *                }
 *        401:
 *          description: 参数错误
 * */
router.post("/delete", async (ctx) => {
    const { id } = ctx.request.body
    AssertUtils.StringNotEmpty(id, "请输入id")
    await deleteCar(id)
    ctx.result.success('删除成功')
})


module.exports = router
